package ods_field

import (
	"errors"
	"fmt"
	"server/global"
)

type OdsDbField struct {
	global.BaseModel
	OdbType string `json:"odbType" form:"odbType" gorm:"not null;column:odbType;comment:源库类型;size:50;"`
	OdbId   uint   `json:"odbId" form:"odbId" gorm:"column:odbId;comment:源库id;size:100;"`
	OdbSchema string `json:"odbSchema" form:"odbSchema" gorm:"not null;column:odbSchema;comment:源模式名;size:50;"`
	OdbTbName string `json:"odbTbName" form:"odbTbName" gorm:"not null;column:odbTbName;comment:源表名;size:50;"`
	Bsm string `json:"bsm" form:"bsm" gorm:"not null;column:bsm;comment:标识码;size:100;"`


	ColName string `json:"colName" form:"colName" gorm:"column:colName;comment:字段名;size:50;"`
	ColType string `json:"colType" form:"colType" gorm:"column:colType;comment:列类型;size:191;"`
	CharSet string `json:"charSet" form:"charSet" gorm:"column:charSet;comment:字符集;size:191;"`
	IsNull  string `json:"isNull" form:"isNull" gorm:"column:isNull;comment:可否为空;size:50;"`
	ColKey  string `json:"colKey" form:"colKey" gorm:"column:colKey;comment:是否主键;size:191;"`
	Privileges string `json:"privileges" form:"privileges" gorm:"column:privileges;comment:用户权限;size:255;"`
	ColComment string `json:"colComment" form:"colComment" gorm:"column:colComment;comment:字段注释;size:100;"`

	DataType   string `json:"dataType" form:"dataType" gorm:"column:dataType;comment:数据类型;size:50;"`
	DataLength int    `json:"dataLength" form:"dataLength" gorm:"column:dataLength;comment:数据长度;size:50;"`

	ForeignKey string `json:"foreignKey" form:"foreignKey" gorm:"column:foreignKey;comment:是否外键;size:250;"`
	Index      string `json:"index" form:"index" gorm:"column:index;comment:是否索引;size:200;"`

	MaxLength   int64  `json:"maxLength" form:"maxLength" gorm:"column:maxLength;comment:字段最大长度;size:100;"`
	ExampleData string `json:"exampleData" form:"exampleData" gorm:"column:exampleData;comment:示例数据;size:200;"`
	OdsTbId   uint   `json:"odsTbId" form:"odsTbId" gorm:"column:odsTbId;comment:ods表id;size:100;"`
	OdsSchema string `json:"odsSchema" form:"odsSchema" gorm:"not null;column:odsSchema;comment:ods模式名;size:50;"`
	OdsTbName string `json:"odsTbName" form:"odsTbName" gorm:"not null;column:odsTbName;comment:ods表名称;size:100;"`
	OdsColName    string `json:"odsColName" form:"odsColName" gorm:"not null;column:odsColName;comment:ods列名称;size:100;"`
	OdsColType    string `json:"odsColType" form:"odsColType" gorm:"not null;column:odsColType;comment:ods列类型;size:191;"`
	OdsCharSet    string `json:"odsCharSet" form:"odsCharSet" gorm:"column:odsCharSet;comment:ods字符集;size:191;"`
	OdsColComment string `json:"odsColComment" form:"odsColComment" gorm:"column:odsColComment;comment:ods字段注释;size:100;"`
	ColDataType   string `json:"colDataType" form:"colDataType" gorm:"column:colDataType;comment:源数据类型;size:50;"`
	ColConv       string `json:"colConv" form:"colConv" gorm:"column:colConv;comment:源数据类型转换;size:50;"`
	OdsDataType   string `json:"odsDataType" form:"odsDataType" gorm:"not null;column:odsDataType;comment:ods数据类型;size:50;"`
	OdsDataLength int    `json:"odsDataLength" form:"odsDataLength" gorm:"not null;column:odsDataLength;comment:ods数据长度;size:50;"`
	OdsColLabel   string `json:"odsLabel" form:"odsLabel" gorm:"column:odsLabel;comment:ods字段标签;size:50;"`
	Selected      bool   `json:"selected" form:"selected" gorm:"-"`

	Remarks string `json:"remarks" form:"remarks" gorm:"column:remarks;comment:备注;size:250;"`
	TimeFormat string `json:"timeFormat" form:"timeFormat" gorm:"column:timeFormat;comment:时间格式;size:50;"`
	Unique     string `json:"unique" form:"unique" gorm:"column:unique;comment:是否唯一;size:100;"`
}

func (m *OdsDbField) TableName() string {
	return "ods_db_field"
}

func (m *OdsDbField) Create() error {
	err := global.SysDB.Save(m).Error
	return err
}

func (m *OdsDbField) Save() error {
	err := global.SysDB.Save(&m).Error
	return err
}

func (m *OdsDbField) SetBsm() error {
	if m.OdbId > 0 && m.OdbSchema != "" && m.OdbTbName != "" && m.ColName != "" {
		m.Bsm = fmt.Sprintf("%d_%s_%s_%s", m.OdbId, m.OdbSchema, m.OdbTbName, m.ColName)
		return nil
	}
	return errors.New("没有SrcDbId或是srcSchemaName为空")
}
